#include "book_service_data.h"

void BookServiceData::save(Book& bookfile)
{
	char path[256] = {};
	sprintf(path,"../data/book/%d.txt",bookfile.m_id);
	FILE* fwp = fopen(path, "w");
	fprintf(fwp,"书名:%s\n",bookfile.m_name.c_str());
	fprintf(fwp,"作者:%s\n",bookfile.m_author.c_str());
	fprintf(fwp,"书号:%u\n",bookfile.m_id);
	fprintf(fwp,"类别:%s\n",bookfile.m_type.c_str());
	time_t time_sec = mktime(&bookfile.m_intime);
	fprintf(fwp,"入库时间:%ld\n",time_sec);
	fprintf(fwp,"剩余数量:%u\n",bookfile.m_amout);
	fprintf(fwp,"总数:%u\n",bookfile.m_total);
	fprintf(fwp,"评分:%lf\n",bookfile.m_score);
	fprintf(fwp,"评分人数:%u\n",bookfile.m_sco_num);
	fprintf(fwp,"推荐:%u\n",bookfile.m_recom);
	fprintf(fwp,"上架状态:%d\n",bookfile.m_isShelf);
	fprintf(fwp,"简介:%s\n",bookfile.m_syn.c_str());
	
	fprintf(fwp,"预定记录数:%u\n",bookfile.m_reserve.size());
	for(unsigned int i=0;i<bookfile.m_reserve.size();i++)
	{
		fprintf(fwp,"预定时间:%ld ", mktime(&bookfile.m_reserve.at(i).m_time) );
		fprintf(fwp,"书号:%u ",bookfile.m_reserve.at(i).m_bookId);
		fprintf(fwp,"账号:%u\n",bookfile.m_reserve.at(i).m_id);
	}
	
	fprintf(fwp,"借书记录数:%u\n",bookfile.m_record.size());
	for(unsigned int i=0;i<bookfile.m_record.size();i++)
	{
		fprintf(fwp,"编号:%u ",bookfile.m_record.at(i).m_id);
		fprintf(fwp,"书号:%u ",bookfile.m_record.at(i).m_bookId);
		fprintf(fwp,"书名:%s ",bookfile.m_record.at(i).m_bookName.c_str());
		fprintf(fwp,"借书人ID:%u ",bookfile.m_record.at(i).m_perId);
		fprintf(fwp,"借书人姓名:%s ",bookfile.m_record.at(i).m_name.c_str());
		fprintf(fwp,"租借天数:%u ",bookfile.m_record.at(i).m_day);
		fprintf(fwp,"借书日期:%ld ",mktime(&bookfile.m_record.at(i).m_broTime));
		fprintf(fwp,"是否归还:%d ",bookfile.m_record.at(i).m_isReturn);
		if(bookfile.m_record.at(i).m_isReturn)
		{
			fprintf(fwp,"归还日期:%ld ",mktime(&bookfile.m_record.at(i).m_retTime));
		}
		fprintf(fwp,"\n");
	}
	fprintf(fwp,"评论数:%u\n",bookfile.m_vecCom.size());
	for(unsigned int i=0;i<bookfile.m_vecCom.size();i++)
	{
		fprintf(fwp,"编号:%u ",bookfile.m_vecCom.at(i).m_id);
		fprintf(fwp,"用户:%s ",bookfile.m_vecCom.at(i).m_name.c_str());
		fprintf(fwp,"时间:%ld ",mktime(&bookfile.m_vecCom.at(i).m_time));
		fprintf(fwp,"评分:%u\n",bookfile.m_vecCom.at(i).m_score);
		fprintf(fwp,"内容:%s\n",bookfile.m_vecCom.at(i).m_content.c_str());
	}
	fclose(fwp);
}
Book* BookServiceData::load(unsigned int id)
{
	char path[256] = {};
	sprintf(path,"../data/book/%d.txt",id);
	FILE* frp = fopen(path, "r");
	if(frp == NULL)
	{
		return NULL;
	}
	Book* bookfile = new Book;
	char buf[256] = {};
	fscanf(frp,"书名:%s\n",buf);
	bookfile->m_name = buf;
	bzero(buf,256);
	fscanf(frp,"作者:%s\n",buf);
	bookfile->m_author = buf;
	bzero(buf,256);
	fscanf(frp,"书号:%u\n",&bookfile->m_id);
	fscanf(frp,"类别:%s\n",buf);
	bookfile->m_type = buf;
	time_t r_time = 0;
	fscanf(frp,"入库时间:%ld\n",&r_time);
	struct tm *t = localtime(&r_time);
	bookfile->m_intime = *t;
	//free(t);
	fscanf(frp,"剩余数量:%u\n",&bookfile->m_amout);
	fscanf(frp,"总数:%u\n",&bookfile->m_total);
	fscanf(frp,"评分:%lf\n",&bookfile->m_score);
	fscanf(frp,"评分人数:%u\n",&bookfile->m_sco_num);
	fscanf(frp,"推荐:%u\n",&bookfile->m_recom);
	int isShelf = 0;
	fscanf(frp,"上架状态:%d\n",&isShelf);
	bookfile->m_isShelf = isShelf?1:0;
	char buf1[4096] = {};
	fscanf(frp,"简介:%s\n",buf1);
	bookfile->m_syn = buf1;
	int num = 0;
	fscanf(frp,"预定记录数:%u\n",&num);
	for(int i=0;i<num;i++)
	{
		Book::Reserve reserve;
		time_t tt = 0;
		fscanf(frp,"预定时间:%ld ", &tt );
		struct tm *t = localtime(&tt);
		reserve.m_time = *t;
		//free(t);
		fscanf(frp,"书号:%u ",&reserve.m_bookId);
		fscanf(frp,"账号:%u\n",&reserve.m_id);
		bookfile->m_reserve.push_back(reserve);
	}
	fscanf(frp,"借书记录数:%u\n",&bookfile->m_numRecord);
	for(unsigned int i=0;i<bookfile->m_numRecord;i++)
	{
		Record record;
		fscanf(frp,"编号:%u ",&record.m_id);
		fscanf(frp,"书号:%u ",&record.m_bookId);
		char buf[256] = {};
		fscanf(frp,"书名:%s ",buf);
		record.m_bookName = buf;
		bzero(buf,256);
		fscanf(frp,"借书人ID:%u ",&record.m_perId);
		fscanf(frp,"借书人姓名:%s ",buf);
		record.m_name = buf;
		fscanf(frp,"租借天数:%u ",&record.m_day);
		time_t r_time = 0;
		fscanf(frp,"借书日期:%ld ",&r_time);
		struct tm *t = localtime(&r_time);
		record.m_broTime  = *t;
		//free(t);
		int isReturn = 0;
		fscanf(frp,"是否归还:%d ",&isReturn);
		record.m_isReturn = isReturn?1:0;
		if(isReturn)
		{
			time_t r_time = 0;
			fscanf(frp,"归还日期:%ld ",&r_time);
			struct tm *t = localtime(&r_time);
			record.m_retTime  = *t;
			//free(t);
		}
		fscanf(frp,"\n");
		bookfile->m_record.push_back(record);
	}
	
	fscanf(frp,"评论数:%u\n",&bookfile->m_numCom);
	for(unsigned int i=0;i< bookfile->m_numCom;i++)
	{
		Comment comment;
		fscanf(frp,"编号:%u ",&comment.m_id);
		bzero(buf,256);
		fscanf(frp,"用户:%s ",buf);
		comment.m_name = buf;
		time_t r_time = 0;
		fscanf(frp,"时间:%ld ",&r_time);
		struct tm *t = localtime(&r_time);
		comment.m_time  = *t;
		//free(t);
		fscanf(frp,"评分:%u\n",&comment.m_score);
		bzero(buf1,4096);
		fscanf(frp,"内容:%s\n",buf1);
		comment.m_content = buf1;
		bookfile->m_vecCom.push_back(comment);
	}
	fclose(frp);
	return bookfile;
	
}
